package com.ynt.hangqing.service.impl;

import com.ynt.hangqing.entity.Gxstj;
import com.ynt.hangqing.entity.ShaTangJu;
import com.ynt.hangqing.mapper.GxstjMapper;
import com.ynt.hangqing.mapper.ShaTangJuMapper;
import com.ynt.hangqing.service.ShaTangJuService;
import com.ynt.hangqing.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


@Service
@Slf4j
public class ShaTangJuServiceImpl implements ShaTangJuService {


    @Autowired
    ShaTangJuMapper shaTangJuMapper;

    @Autowired
    GxstjMapper gxstjMapper;

    @Override
    public void fetchSTJInfo() {
        log.info("抓取广西砂糖橘价格信息=======start====");

        long startTime = System.currentTimeMillis();
        String startUrl = "https://www.nyw58.com/hangqing/list-163-";
        int endPage = 38;
        try {
            for (int i = 1; i <= endPage; i++) {
                String currentPageUrl = startUrl + i + ".html";
                System.out.println("当前爬取列表页数==== " + i + " ====");
                System.out.println("当前列表页面url====================================" + currentPageUrl);

                Document doc = parsePage(currentPageUrl);
                Elements pages = doc.getElementsByClass("newslist");
                Element element = pages.get(0);
                Elements as = element.getElementsByTag("a");

                System.out.println("当前列表页面记录条数========>" + as.size());
                for (Element a : as) {
                    String detailPageUrl = a.attr("href"); //详情页url
                    System.out.println("当前解析详情页页面地址:=============>" + detailPageUrl);

                    //解析详情页
                    Document detailDoc = parsePage(detailPageUrl);
                    Elements tbody = detailDoc.getElementsByTag("tbody");
                    Element table = tbody.get(0);

                    Elements trs = table.getElementsByTag("tr");
                    for (Element tr : trs) {
                        try {
                            Elements tds = tr.getElementsByTag("td");
                            if (tds.size() == 7) {
                                String productName = tds.get(0).text();    //商品名称(砂糖橘)
                                String specification = tds.get(1).text();  //规格
                                String maxPrice = tds.get(2).text();   //最高价
                                String minPrice = tds.get(3).text();   //最低价
                                String avgPrice = tds.get(4).text();   //均价
                                String marketName = tds.get(5).text();     //市场名称
                                String marketDate = tds.get(6).text();       //市场时间

                                if ("-".equals(maxPrice)) maxPrice = "0";
                                if ("-".equals(minPrice)) minPrice = "0";
                                if ("-".equals(avgPrice)) avgPrice = "0";

                                ShaTangJu stj = new ShaTangJu();
                                stj.setProductName(productName);
                                stj.setSpecification(specification);
                                stj.setMaxPrice(new BigDecimal(maxPrice));
                                stj.setMinPrice(new BigDecimal(minPrice));
                                stj.setAvgPrice(new BigDecimal(avgPrice));
                                stj.setMarketName(marketName);
                                stj.setMarketDate(DateUtil.parseDate(marketDate));
                                shaTangJuMapper.insert(stj);
                            } else if (tds.size() == 5) {
                                String productName = tds.get(0).text();    //商品名称(砂糖橘)
                                String marketName = tds.get(1).text();     //市场名称
                                String maxPrice = tds.get(2).text();   //最高价
                                String minPrice = tds.get(3).text();   //最低价
                                String avgPrice = tds.get(4).text();   //均价

                                if ("-".equals(maxPrice)) maxPrice = "0";
                                if ("-".equals(minPrice)) minPrice = "0";
                                if ("-".equals(avgPrice)) avgPrice = "0";

                                Elements marketDateE = detailDoc.getElementsByClass("info");
                                Element child = marketDateE.get(0);
                                String text = child.text();
                                //   日期：2014-10-27     浏览：609    |


                                String marketDate = text.substring(text.indexOf("日期：") + 3, text.indexOf("浏览")).trim();       //市场时间

                                ShaTangJu stj = new ShaTangJu();
                                stj.setProductName(productName);
                                stj.setMaxPrice(new BigDecimal(maxPrice));
                                stj.setMinPrice(new BigDecimal(minPrice));
                                stj.setAvgPrice(new BigDecimal(avgPrice));
                                stj.setMarketName(marketName);
                                stj.setMarketDate(DateUtil.parseDate(marketDate));
                                shaTangJuMapper.insert(stj);
                            }

                        } catch (IndexOutOfBoundsException e) {
                            log.error("当前页面解析砂糖橘信息报数组下标越界,页面链接: " + detailPageUrl);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("抓取砂糖橘价格信息=======end======耗时: " + (System.currentTimeMillis() - startTime));
    }


    @Override
    public void fetchGxSTJ() {

        log.info("抓取砂糖橘价格信息=======start====");

        long startTime = System.currentTimeMillis();
        String startUrl = "https://www.nyw58.com/hangqing/list-1540-";
        int endPage = 5;
        try {
            for (int i = 1; i <= endPage; i++) {
                String currentPageUrl = startUrl + i + ".html";
                System.out.println("当前爬取列表页数==== " + i + " ====");
                System.out.println("当前列表页面url====================================" + currentPageUrl);

                Document doc = parsePage(currentPageUrl);
                Elements pages = doc.getElementsByClass("newslist");
                Element element = pages.get(0);
                Elements as = element.getElementsByTag("a");

                System.out.println("当前列表页面记录条数========>" + as.size());
                for (Element a : as) {
                    String detailPageUrl = a.attr("href"); //详情页url
                    System.out.println("当前解析详情页页面地址:=============>" + detailPageUrl);

                    //解析详情页
                    Document detailDoc = parsePage(detailPageUrl);
                    Elements tbody = detailDoc.getElementsByTag("tbody");
                    Element table = tbody.get(0);

                    Elements trs = table.getElementsByTag("tr");
                    for (Element tr : trs) {
                        try {
                            Elements tds = tr.getElementsByTag("td");
                            String place = tds.get(1).text();       //产地
                            String price = tds.get(2).text();       //价格
                            String detail = tds.get(3).text();      //详情

                            Elements marketDateE = detailDoc.getElementsByClass("info");
                            Element child = marketDateE.get(0);
                            String text = child.text();
                            //   日期：2014-10-27     浏览：609    |
                            String marketDate = text.substring(text.indexOf("日期：") + 3, text.indexOf("浏览")).trim();       //市场时间

                            Gxstj gxstj = new Gxstj();
                            gxstj.setPlace(place);
                            gxstj.setPrice(price);
                            gxstj.setDetail(detail);
                            gxstj.setMarketDate(marketDate);
                            gxstjMapper.insert(gxstj);
                        } catch (IndexOutOfBoundsException e) {
                            log.error("当前页面解析砂糖橘信息报数组下标越界,页面链接: " + detailPageUrl);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("抓取砂糖橘价格信息=======end======耗时: " + (System.currentTimeMillis() - startTime));
    }


    /**
     * 解析页面
     */
    private Document parsePage(String url) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity, "utf-8");
        response.close();
        return Jsoup.parse(content);
    }

}
